package com.common.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @create 2020-11-21 17:44
 */
@Data
@ApiModel(value="统一响应格式", description="统一响应格式")
@JsonInclude(JsonInclude.Include.NON_NULL)  //如果data为空，则不显示
public class ResponseResult {

    @ApiModelProperty(value = "是否成功")
    private boolean success;

    @ApiModelProperty(value = "响应码")
    private Integer code;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Object data;

    private ResponseResult(){}

    public static ResponseResult success(Code code) {
        ResponseResult result = new ResponseResult();
        result.setSuccess(true);
        result.setCode(code.getCode());
        result.setMessage(code.getMessage());
        return result;
    }

    public static ResponseResult error(Code code) {
        ResponseResult result = new ResponseResult();
        result.setSuccess(false);
        result.setCode(code.getCode());
        result.setMessage(code.getMessage());
        return result;
    }

    public ResponseResult Data(Object map) {
        this.setData(map);
        return this;
    }

    public ResponseResult Data(String key, Object value) {
        Map<String, Object> map = new HashMap<>();
        map.put(key, value);
        this.setData(map);
        System.out.println(this.data);
        return this;
    }

    public ResponseResult Message(String message) {
        this.setMessage(message);
        return this;
    }
}
